#!/usr/bin/env python
#
#   Copyright (C) 2006 Jonathan Saggau                                     
#   saggau@gmail.com                                                       
#                                                                          
#   This program is free software; you can redistribute it and/or modify  
#   it under the terms of the GNU General Public License as published by  
#   the Free Software Foundation; either version 2 of the License, or     
#   (at your option) any later version.                                    
#                                                                          
#   This program is distributed in the hope that it will be useful,        
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
#   GNU General Public License for more details.                          
#                                                                          
#   You should have received a copy of the GNU General Public License     
#   along with this program; if not, write to the                         
#   Free Software Foundation, Inc.,                                        
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  
#

import objc
from Foundation import *
# starts up some debugging stuff

from PyObjCTools import NibClassBuilder
from supyrconductor import buffer as primitiveBuffer
import scosc
import alerts

NibClassBuilder.extractClasses("MainMenu")

class Buffer(NibClassBuilder.AutoBaseClass):
    """big pyobjc wrapper class of getters and setters for the cross plat 
    buffer model in supyrconductor.  Makes it KVC complient for cocoa."""

    def init(self):
        raise Exception, """Can\'t init with init.
                          Use initWithController_BufferID_FilePath_"""

    def initWithController_BufferID_FilePath_(self, controller, 
                                              bufferID, path):
        self = super(Buffer, self).init() 
        if self is None: return None
        self.buffer = primitiveBuffer.Buffer(controller, bufferID, path)
        if bufferID and path:
            try:
                self.buffer.allocRead()
            except IOError, e:
                alerts.alertMsgNoSheet(title = "Alert", 
                                       message = "Error: %s" %e, 
                                       buttonString = "OK")
        self.lastPath = None
        self.lastInfo = {'BufferNumber':bufferID,
                         'numberofFrames':0,
                         'numberOfChannels':0,
                         'sampleRate':0}
        return self

    def query(self):
        if self.lastPath != self.buffer.filePath:
        #path has changed
            self.lastPath = self.buffer.filePath
            try:
                self.lastInfo = self.buffer.query()
            except IOError, e:
                pass
            if '/fail' in self.lastInfo.keys():
                self.lastInfo = {'BufferNumber':bufferID,
                                 'numberofFrames':0,
                                 'numberOfChannels':0,
                                 'sampleRate':0}
                # if the buffer isn't there, pretend it's empty.
                # The primitive buffer object doesn't send messages
                # to a buffer it didn't allocate, so we won't mess up
                # the server here.
        return self.lastInfo
                # info should always be a dictionary with the same keys
                # as the return dictionary above
            
    def ID(self):
        id = self.buffer.ID
        if id:
            return id
        else:
            return 0
        
    def filePath(self):
        return self.buffer.filePath

    def close(self):
        self.buffer.close()